﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDAPIOLELib;

namespace QCCure.Core
{
    [Serializable]
    public class QCBug
    {
        private IBug bug;
        
        private string id;
        private string status;
        private string summary;
        private string assignedTo;
        private string detectedBy;
        private string priority;
        private string project;
        
        private bool isLocked;
        private bool modified;
        private bool autoPost;
        private bool hasAttachment;
        private bool hasChange;
        private bool isVirtual;

        public QCBug()
        {
        }

        public QCBug(IBug bug)
        {
            this.bug = bug;
            Refresh();
        }

        public string ID { get { return id; } }

        public string Status
        {
            get { return status; }
            set
            {
                if (bug != null)
                {
                    this.status = value;
                    bug.Status = value;
                }
                else
                {
                    throw new Exception("Unable to set a status, no bug selected");
                }
            }
        }
        public string Summary { get { return summary; } }

        public string AssignedTo {
            get { return assignedTo; } 
            set {
                if (bug != null)
                {
                    this.assignedTo = value;
                    bug.AssignedTo = value;
                }
                else
                {
                    throw new Exception("Unable to set a AssignedTo, no bug selected");
                }
            }
        }
        public string DetectedBy { get { return detectedBy; } }
        
        public string Priority { get { return priority; } }
        public string Project { get { return project; } }

        public bool IsLocked { get { return isLocked; } }
        public bool Modified { get { return modified; } }
        public bool AutoPost { get { return autoPost; } }
        public bool HasAttachment { get { return hasAttachment; } }
        public bool HasChange { get { return hasChange; } }
        public bool Virtual { get { return isVirtual; } }

        IList ChangeLinks
        {
            get
            {
                return bug.ChangeLinks;
            }
        }

        public IList<QCAttachment> Attachments
        {
            get
            {
                IList<QCAttachment> qcQttachment = new List<QCAttachment>();

                AttachmentFactory factory = bug.Attachments as AttachmentFactory;
                
                List attachments = factory.NewList(factory.Filter.Text);
                foreach (IAttachment attachment in attachments)
                {
                    qcQttachment.Add(new QCAttachment(attachment));
                }

                return qcQttachment;
            }
        }

        public IList<IHistoryRecord> History
        { 
            get {
                return (IList<IHistoryRecord>)bug.History; 
            }
        }

        public string this[string fieldName]
        {
            get
            {
                return bug != null ? bug[fieldName].ToString() : null;
            }
            set
            {
                if (bug != null)
                {
                    bug[fieldName] = value;
                }
                else
                {
                    throw new Exception("Unable to set " + fieldName + ", no bug selected");
                }
            }
        }
        
        public IList<QCBug> FindSimilarBugs(int SimilarityRatio = 10)
        {
            IList<QCBug> bugs = new List<QCBug>();
            if (bug == null)
            {
                return bugs;
            }

            IList list = bug.FindSimilarBugs(SimilarityRatio);
            foreach (Bug qcBug in list)
            {
                bugs.Add(new QCBug(qcBug));
            }
            return bugs;
        }

        public bool LockObject()
        {
            if (bug != null)
            {
                return bug.LockObject();
            }
            else
            {
                throw new Exception("Unable to LockObject, no bug selected");
            }
        }

        public void Mail(string SendTo, string SendCc = "", int Option = 0, string Subject = "", string Comment = "")
        {
            bug.Mail(SendTo, SendCc, Option, Subject, Comment);
        }

        public void Post()
        {
            bug.Post();
        }

        public void Refresh()
        {
            if (bug != null)
            {
                id = bug.ID.ToString();
                status = bug.Status;
                summary = bug.Summary;
                assignedTo = bug.AssignedTo;
                detectedBy = bug.DetectedBy;
                priority = bug.Priority;
                project = bug.Project;

                isLocked = bug.IsLocked;
                modified = bug.Modified;
                autoPost = bug.AutoPost;
                hasAttachment = bug.HasAttachment;
                hasChange = bug.HasChange;
                isVirtual = bug.Virtual;

            } else {

                id = null;
                status = null;
                summary = null;
                assignedTo = null;
                detectedBy = null;
                priority = null;
                project = null;

                isLocked = false;
                modified = false;
                autoPost = false;
                hasAttachment = false;
                hasChange = false;
                isVirtual = false;
            }

        }

        public void QCRefresh()
        {
            bug.Refresh();
            Refresh();
        }

        public void Undo()
        {
            bug.Undo();
        }
        
        public void UnLockObject()
        {
            bug.UnLockObject();
        }
        
        public override string ToString()
        {
            return ID + "\t" + AssignedTo + "\t" + Summary + "\t";
        }
    }
}
